package com.pingf.estimation.action;

import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.persistent.query.EvaluatedLevelQuery;
import com.base.common.utils.EasyUtil;
import com.cjy.common.bean.OpAttributes;
import com.pingf.estimation.service.EstimationService;
import com.ygj.dept.service.DeptService;
import org.apache.ibatis.executor.ExecutorException;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import javax.annotation.Resource;

import static com.base.common.GlobaConstants.*;

/**
 * 评分action
 *
 * @author Kat
 * @datetime 2017.06.10 14:36
 */
@Action("estimationAction")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class EstimationAction extends AbstractBaseAction {

    @Resource
    private EstimationService estimationService;

    private int levelValue;

    private String terminalCode;
    private int employeeId;
    private String phone;
    private int compoundsId;
    private int evaluatePointId;
    private String queryTime;

    public String getQueryTime() {
        return queryTime;
    }

    public void setQueryTime(String queryTime) {
        this.queryTime = queryTime;
    }

    public int getEvaluatePointId() {
        return evaluatePointId;
    }

    public void setEvaluatePointId(int evaluatePointId) {
        this.evaluatePointId = evaluatePointId;
    }

    public int getCompoundsId() {
        return compoundsId;
    }

    public void setCompoundsId(int compoundsId) {
        this.compoundsId = compoundsId;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public int getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    }

    public String getTerminalCode() {
        return terminalCode;
    }

    public void setTerminalCode(String terminalCode) {
        this.terminalCode = terminalCode;
    }

    public int getLevelValue() {
        return levelValue;
    }

    public void setLevelValue(int levelValue) {
        this.levelValue = levelValue;
    }

    public String queryEstimationLevelList(){
        try {
            EvaluatedLevelQuery evaluatedLevelQuery = new EvaluatedLevelQuery();
            evaluatedLevelQuery.setStatus(1);
            actionResult = ActionResult.getActionResult(new OpAttributes<>().add("code", 1).add("result", estimationService.queryLevelList(evaluatedLevelQuery)));
        } catch (Exception e) {
            e.printStackTrace();
            actionResult = ActionResult.getActionResult(new OpAttributes<>().add("code", 0));
        }
        return returnType();
    }

    public String evaluate(){
        try {

            actionResult = ActionResult.getActionResult(new OpAttributes<>().add("code", estimationService.evaluate(levelValue, terminalCode.trim(), employeeId)));
        } catch (Exception e) {
            e.printStackTrace();
            actionResult = ActionResult.getActionResult(new OpAttributes<>().add("code", 0));
        }
        return returnType();
    }

    public String queryCurrentEmployeeInfo() {
        try {
            actionResult = ActionResult.getActionResult(new OpAttributes<>().add("code", 1).add("result", estimationService.queryCurrentEmployeeInfo(EasyUtil.rc4(phone, "szcjy").trim())));
        } catch (Exception e) {
            e.printStackTrace();
            actionResult = ActionResult.getActionResult(new OpAttributes<>().add("code", 0));
        }
        return  returnType();
    }

    public String queryEvaluatePoints() {
        try {
            actionResult = ActionResult.getActionResult(new OpAttributes<>().add("code", 1).add("result", estimationService.queryEvaluatePoints(compoundsId)));
        } catch (Exception e) {
            actionResult = ActionResult.getActionResult(new OpAttributes<>().add("code", 0));
            e.printStackTrace();
        }
        return returnType();
    }

    public String queryTotalEvaluationsListByEvaluatePoint() {
        try {
            actionResult = ActionResult.getActionResult(new OpAttributes<>().add("code", 1).add("result", estimationService.queryTotalEvaluationsListByEvaluatePoint(evaluatePointId, queryTime)));
        } catch (Exception e) {
            actionResult = ActionResult.getActionResult(new OpAttributes<>().add("code", 0));
            e.printStackTrace();
        }
        return returnType();
    }
}
